//
//  ZBNavigationController.m
//  ZBNavigator
//
//  Created by xzb on 2018/5/30.
//

#import "ZBNavigationController.h"

@interface ZBNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation ZBNavigationController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.interactivePopGestureRecognizer.delegate = self;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([self.viewControllers firstObject]) viewController.hidesBottomBarWhenPushed = YES;
    [super pushViewController:viewController animated:animated];
}

- (UIViewController*)topVisibleViewController
{
    UIViewController *visibleViewController = self.visibleViewController;
    while (visibleViewController.presentedViewController) {
        visibleViewController = visibleViewController.presentedViewController;
    }
    return visibleViewController;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (self.viewControllers.count <= 1) {
        return NO;
    }
    if ([self.topViewController respondsToSelector:@selector(zb_shouldPanback)]) {
        return [(id <ZBNavigationDelegate>)self.topViewController zb_shouldPanback];
    }
    
    return YES;
}

#pragma mark - api
- (void)setZb_backgroundImage:(UIImage *)zb_backgroundImage {
    
    _zb_backgroundImage = zb_backgroundImage;
    
    [self.navigationBar setBackgroundImage:zb_backgroundImage
                             forBarMetrics:UIBarMetricsDefault];
}

- (void)setZb_tintColor:(UIColor *)zb_tintColor {
    
    _zb_tintColor = zb_tintColor;
    
    [self.navigationBar setTintColor:zb_tintColor];
}

- (void)setZb_foregroundColor:(UIColor *)zb_foregroundColor {
    
    _zb_foregroundColor = zb_foregroundColor;
    
    NSDictionary *zb_dictionary = @{NSForegroundColorAttributeName : zb_foregroundColor};
    
    [self.navigationBar setTitleTextAttributes:zb_dictionary];
}

- (void)setZb_shadowImage:(UIImage *)zb_shadowImage {
    
    _zb_shadowImage = zb_shadowImage;
    
    [self.navigationBar setShadowImage:zb_shadowImage];
}
@end
